﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TownCrier.Infrastructure.Entities;
using TownCrier.Infrastructure.Contracts;
using Moq;
//using Twitterizer;
using RestSharp;
using TownCrier.Channels;
using TownCrier.Channels.ChannelTools;

namespace TownCrier.Tests.Channels {
    /// <summary>
    /// Summary description for TwitterChannelTests
    /// </summary>
    [TestClass]
    public class TwitterChannelTests : RestChannelTestBase {

        //private string tokens;
        //private Event _sourceEvent;
        private TwitterChannelSection _settings;

        public TwitterChannelTests() : base() {
            _settings = System.Configuration.ConfigurationManager.GetSection("TwitterChannelSection") as TwitterChannelSection;
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            string apppath = @"..\..\..\TownCrier.Tests\intpages";
            _server = new CassiniDev.CassiniDevServer();
            _server.StartServer(apppath, 12345, "/", "localhost");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup() {
            if (_server != null) {
                _server.StopServer();
                _server.Dispose();
            }
        }
        //
        // Use TestInitialize to run code before running each test 
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        //[TestInitialize()]
        //public void MyTestInitialize() {
        //    DateTime eventstart = DateTime.UtcNow.AddDays(2);
        //    _sourceEvent = new Event {
        //        Title = "Test event", Topic = "Moles", Start = eventstart, End = eventstart.AddHours(4),
        //        URL = new Uri("http://www.anthroserbia.org"),
        //        UserGroup = "Dev UG",
        //        Venue = new Venue {
        //            Name = "MSO",
        //            City = "Belgrade",
        //            Street = "Makedonska 30",
        //        }
        //    };
        
        //}



        [TestMethod]
        public void TwitterChannelShouldImplementIChannelInterface() {
            TwitterChannel tc = new TwitterChannel();

            Assert.IsInstanceOfType(tc, typeof(IChannel));
        }
        
        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void TwitterChannelShouldThrowAnExceptionWhenEventIsInvalid() {
            //Assert.Fail("Needs to be implemented");
            TwitterChannel tc = new TwitterChannel();

            tc.TransmitMessage(null);
        }

        [TestMethod]
        public void TwitterChannelShouldDeliverAnEvent() {
            var channel = new TwitterChannel(_client, _request, "TwitterChannelSection");

            var result = channel.TransmitMessage(_sourceEvent);

            Assert.IsTrue(result.Success);

        }

        [TestMethod]
        public void TwitterChannelShouldReturnFalseForInvalidDelivery() {
            //Assert.Fail("Needs to be implemented");
            var section = new TwitterChannelSection();
            section.ApiBaseUrl.Value = "http://tempuri.org";
            section.ApiPostResource.Value = "/test.ashx?status={status}";
            section.OauthToken = _settings.OauthToken;

            var channel = new TwitterChannel(_client, _request, section);
            var result = channel.TransmitMessage(_sourceEvent);

            Assert.IsFalse(result.Success);
        }

        [TestMethod]
        public void TwitterChannelResultShouldContainIdOfTweet() {
            var channel = new TwitterChannel();

            var result = channel.TransmitMessage(_sourceEvent);

            Assert.IsTrue(result.Message.Contains("\"id\":"));
        }

        [TestMethod]
        public void TwitterChannelDeliveryResultShouldContainCorrectChannelName() {
            var channel = new TwitterChannel();

            var result = channel.TransmitMessage(_sourceEvent);

            Assert.AreEqual("Twitter Channel", result.DeliveryChannel);

        }


    }
}
